﻿Imports System.Data.SqlClient
Imports System.IO
Imports System.Net.WebRequestMethods

Public Class frmClaimsNew

    Sub Clear()
        Me.txtInsuranceCompany.Clear()
        Me.txtPolicyNo.Clear()
        Me.txtMemberName.Clear()
        Me.txtMedIDPlateNo.Clear()
        Me.CombCarMake.SelectedIndex = -1
        Me.ChkCurrency.Checked = True
        Me.txtClaimAmount.Text = "0.00"
        Me.DTPDateOfLoss.Value = Today.Date
        Me.DTPDateOfReceive.Value = Today.Date
        Me.DTPDateOfSubmission.Value = Today.Date
        Me.DTPDateOfDeadline.Value = Today.Date
        Me.txtNotes.Clear()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button8.Click
        Clear()
    End Sub

    Private Sub frmClaimsNew_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        CheckDeadLine()
        FillCurrencyList()
        FillCarsMake()
    End Sub

    Sub FillCurrencyList()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From ExchangeRates Order By Currency", cnn)
            Dim Reader As SqlDataReader

            Me.CombCurr.Items.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Dim Item As New ListBox
                Me.CombCurr.Items.Add(Reader.Item("Currency"))
            End While
            cnn.Close()

            Me.CombCurr.SelectedItem = "SAR"

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub FillCarsMake()
        Try
            Me.Cursor = Cursors.WaitCursor

            Dim cmd As New SqlCommand("Select * From CarsMake Order By CarsMake", cnn)
            Dim Reader As SqlDataReader

            Me.CombCarMake.Items.Clear()

            cnn.Open()
            Reader = cmd.ExecuteReader
            While Reader.Read
                Dim Item As New ListBox
                Me.CombCarMake.Items.Add(Reader.Item("CarsMake"))
            End While
            cnn.Close()

            Me.Cursor = Cursors.Default
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        If Me.txtPolicyNo.Text.Trim.Length = 0 OrElse Me.txtInsuranceCompany.Text.Trim.Length = 0 Then
            MsgBox("Please select a valid policy")
            Exit Sub
        ElseIf Me.txtClaimAmount.Text.Trim.Length = 0 Then
            MsgBox("Please enter a valid claim amount")
            Exit Sub
        Else
            If Me.RMedical.Checked = True Then
                If Me.txtMemberName.Text.Trim.Length = 0 OrElse Me.txtMedIDPlateNo.Text.Trim.Length = 0 Then
                    MsgBox("Please fill in member name and ID#")
                    Exit Sub
                End If
            ElseIf Me.RMotor.Checked = True Then
                If Me.txtMedIDPlateNo.Text.Trim.Length = 0 Then
                    MsgBox("Please enter car plate no.")
                    Me.txtMedIDPlateNo.Focus()
                    Exit Sub
                End If
            End If
            Try
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand()
                Dim Trans As SqlTransaction
                Dim ClaimNo, Year As Integer
                Dim ClaimType As String

                'Check claim type
                If Me.RMedical.Checked = True Then
                    ClaimType = "MED"
                ElseIf RMotor.Checked = True Then
                    ClaimType = "MOT"
                ElseIf Me.RGeneral.Checked = True Then
                    ClaimType = "GEN"
                End If

                cnn.Open()
                Trans = cnn.BeginTransaction
                cmd.Connection = cnn
                cmd.Transaction = Trans

                cmd.CommandText = "Select IsNull(Max(CNo),0) From Claims Where Year(TransDate)=Year(GetDate()) and ClaimType=N'" & ClaimType & "'"
                ClaimNo = CInt(cmd.ExecuteScalar) + 1

                cmd.CommandText = "Select Year(GetDate())"
                Year = CInt(cmd.ExecuteScalar)

                cmd.CommandText = "Insert Into Claims (CNo,ClaimType,ClaimNo,Status,ClientID,ClientName,InsuranceCompany,PolicyNo,MembName,MedIDCarNo," & _
                                  "CarsMake,ClaimAmount,OtherCurrAmount,OtherCurr,DateOfLoss,DateOfReceive,DateOfSubmission,DateOfDeadline,Notes,SavedUser) Values " & _
                                  "(@CNo,@ClaimType,@ClaimNo,@Status,@ClientID,@ClientName,@InsuranceCompany,@PolicyNo,@MembName,@MedIDCarNo," & _
                                  "@CarsMake,@ClaimAmount,@OtherCurrAmount,@OtherCurr,@DateOfLoss,@DateOfReceive,@DateOfSubmission,@DateOfDeadline,@Notes,@SavedUser)"

                'Add values
                cmd.Parameters.AddWithValue("@CNo", ClaimNo)
                cmd.Parameters.AddWithValue("@ClaimType", ClaimType)
                cmd.Parameters.AddWithValue("@ClaimNo", "C-" & Year & "-" & ClaimType & "-" & ClaimNo)
                cmd.Parameters.AddWithValue("@Status", "Active")
                cmd.Parameters.AddWithValue("@ClientID", Me.txtClientID.Text.Trim)
                cmd.Parameters.AddWithValue("@ClientName", Me.txtClientName.Text)
                cmd.Parameters.AddWithValue("@InsuranceCompany", Me.txtInsuranceCompany.Text)
                cmd.Parameters.AddWithValue("@PolicyNo", Me.txtPolicyNo.Text.Trim)
                cmd.Parameters.AddWithValue("@MembName", Me.txtMemberName.Text.Trim)
                cmd.Parameters.AddWithValue("@MedIDCarNo", Me.txtMedIDPlateNo.Text.Trim)
                cmd.Parameters.AddWithValue("@CarsMake", Me.CombCarMake.Text)
                cmd.Parameters.AddWithValue("@ClaimAmount", CDbl(Me.txtClaimAmount.Text))
                cmd.Parameters.AddWithValue("@OtherCurrAmount", CDbl(Me.txtOtherCurrAmount.Text))
                cmd.Parameters.AddWithValue("@OtherCurr", Me.CombCurr.Text)
                cmd.Parameters.AddWithValue("@DateOfLoss", Me.DTPDateOfLoss.Value.ToShortDateString & " 10:10:10 AM")
                cmd.Parameters.AddWithValue("@DateOfReceive", Me.DTPDateOfReceive.Value.ToShortDateString & " 10:10:10 AM")
                cmd.Parameters.AddWithValue("@DateOfSubmission", Me.DTPDateOfSubmission.Value.ToShortDateString & " 10:10:10 AM")
                cmd.Parameters.AddWithValue("@DateOfDeadline", Me.DTPDateOfDeadline.Value.ToShortDateString & " 10:10:10 AM")
                cmd.Parameters.AddWithValue("@Notes", Me.txtNotes.Text.Trim)
                cmd.Parameters.AddWithValue("@SavedUser", CurrentUser)
                cmd.ExecuteNonQuery()

                Trans.Commit()
                cnn.Close()

                'Create directory to store files
                Dim Dir As New DirectoryInfo(FTPAddress + "Claims")
                Dir.CreateSubdirectory("C-" & Year & "-" & ClaimType & "-" & ClaimNo)

                MsgBox("Saved Successfully")

                PrintClaimLetter("C-" & Year & "-" & ClaimType & "-" & ClaimNo)

                Clear()

                Me.Cursor = Cursors.Default
            Catch ex As Exception
                Me.Cursor = Cursors.Default
                If cnn.State = ConnectionState.Open Then
                    cnn.Close()
                End If
                MsgBox(ex.ToString)
            End Try
        End If
    End Sub

    Private Sub txtClaimAmount_Validated(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtClaimAmount.Validated
        Me.txtClaimAmount.Text = CDbl(Me.txtClaimAmount.Text).ToString("N2")
        CalCurr()
    End Sub

    Private Sub txtClaimAmount_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles txtClaimAmount.Validating
        If Me.txtClaimAmount.Text.Trim.Length = 0 Then
            Me.txtClaimAmount.Text = 0
        End If

        Dim currency As Decimal
        'Convert the current value to currency, with or without a currency symbol.
        If Not Decimal.TryParse(Me.txtClaimAmount.Text, Globalization.NumberStyles.Number, Nothing, currency) Then
            'Don't let the user leave the field if the value is invalid.
            With Me.txtClaimAmount
                .HideSelection = False
                .SelectAll()
                MessageBox.Show("Please enter a valid amount.", _
                                "Invalid Value", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error)
                .HideSelection = True
            End With
            e.Cancel = True
        End If
    End Sub

    Sub CheckDeadLine()
        If Me.RMedical.Checked = True Then
            Me.DTPDateOfDeadline.Value = Me.DTPDateOfSubmission.Value.Date.AddDays(14)
            Me.CombCarMake.SelectedIndex = -1
            Me.CombCarMake.Enabled = False
            Me.txtMemberName.Enabled = True
            Me.txtMedIDPlateNo.Enabled = True

        ElseIf Me.RMotor.Checked = True Then
            Me.CombCarMake.Enabled = True
            Me.DTPDateOfDeadline.Value = Me.DTPDateOfSubmission.Value.Date.AddMonths(1)
            Me.txtMemberName.Enabled = False
            Me.txtMedIDPlateNo.Enabled = True

        ElseIf Me.RGeneral.Checked = True Then
            Me.CombCarMake.SelectedIndex = -1
            Me.CombCarMake.Enabled = False
            Me.txtMemberName.Enabled = False
            Me.txtMedIDPlateNo.Enabled = False
        End If
    End Sub

    Private Sub RMedical_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RMedical.CheckedChanged
        CheckDeadLine()
    End Sub

    Private Sub RMotor_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RMotor.CheckedChanged
        CheckDeadLine()
    End Sub

    Private Sub RGeneral_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RGeneral.CheckedChanged
        CheckDeadLine()
    End Sub

    Private Sub Button2_Click_1(sender As System.Object, e As System.EventArgs) Handles Button2.Click
        Dim a As New frmSelectPolicy
        a.txtClientID.Text = Me.txtClientID.Text
        a.txtClientName.Text = Me.txtClientName.Text
        a.FillPoliciesList()
        a.ShowDialog()

        If SelPolInsurComp <> "" Then
            Me.txtInsuranceCompany.Text = SelPolInsurComp
            Me.txtPolicyNo.Text = SelPolNo
            If SelPolClsOfBus = "Hospitalization" Then
                Me.RMedical.Checked = True
            ElseIf SelPolClsOfBus = "Motor" Then
                Me.RMotor.Checked = True
            Else
                Me.RGeneral.Checked = True
            End If
        End If
    End Sub

    Private Sub txtOtherCurrency_Validated(sender As System.Object, e As System.EventArgs) Handles txtOtherCurrAmount.Validated
        Me.txtOtherCurrAmount.Text = CDbl(Me.txtOtherCurrAmount.Text).ToString("N2")
        CalCurr()
    End Sub

    Private Sub txtOtherCurrency_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtOtherCurrAmount.Validating
        If Me.txtOtherCurrAmount.Text.Trim.Length = 0 Then
            Me.txtOtherCurrAmount.Text = 0
        End If

        Dim currency As Decimal
        'Convert the current value to currency, with or without a currency symbol.
        If Not Decimal.TryParse(Me.txtOtherCurrAmount.Text, Globalization.NumberStyles.Number, Nothing, currency) Then
            'Don't let the user leave the field if the value is invalid.
            With Me.txtOtherCurrAmount
                .HideSelection = False
                .SelectAll()
                MessageBox.Show("Please enter a valid amount.", _
                                "Invalid Value", _
                                MessageBoxButtons.OK, _
                                MessageBoxIcon.Error)
                .HideSelection = True
            End With
            e.Cancel = True
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles ChkCurrency.CheckedChanged
        CheckCurr()
    End Sub

    Sub CheckCurr()
        If Me.ChkCurrency.Checked = True Then
            Me.txtClaimAmount.Enabled = True
            Me.txtClaimAmount.Focus()
            Me.txtOtherCurrAmount.Enabled = False
            Me.CombCurr.Enabled = False
            Me.CombCurr.SelectedItem = "SAR"
            Me.txtExchRate.Enabled = False

        ElseIf Me.ChkCurrency.Checked = False Then
            Me.txtClaimAmount.Enabled = False
            Me.txtOtherCurrAmount.Enabled = True
            Me.txtOtherCurrAmount.Focus()
            Me.CombCurr.Enabled = True
            Me.txtExchRate.Enabled = True
        End If
    End Sub

    Sub CalCurr()
        Try
            If Me.ChkCurrency.Checked = False Then
                If Me.txtOtherCurrAmount.Text.Length = 0 Then
                    Me.txtOtherCurrAmount.Text = 0
                End If

                If Me.txtExchRate.Text.Length = 0 Then
                    Me.txtExchRate.Text = 0
                End If

                Me.txtClaimAmount.Text = CDbl(CDbl(Me.txtOtherCurrAmount.Text.Trim) * CDbl(Me.txtExchRate.Text.Trim)).ToString("N2")

            Else
                Me.txtOtherCurrAmount.Text = Me.txtClaimAmount.Text
                Me.txtExchRate.Text = 1
            End If
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub txtExchRate_Validated(sender As Object, e As System.EventArgs) Handles txtExchRate.Validated
        CalCurr()
    End Sub

    Private Sub txtExchRate_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtExchRate.KeyUp
        If e.KeyCode = Keys.Enter Then
            CalCurr()
        End If
    End Sub

    Private Sub txtOtherCurrAmount_KeyUp(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles txtOtherCurrAmount.KeyUp
        If e.KeyCode = Keys.Enter Then
            CalCurr()
        End If
    End Sub

    Private Sub CombCurr_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CombCurr.SelectedIndexChanged
        Try
            If Me.CombCurr.SelectedIndex = -1 Then
                Me.txtExchRate.Clear()
            Else
                Me.Cursor = Cursors.WaitCursor

                Dim cmd As New SqlCommand("Select Rate From ExchangeRates Where Currency=N'" & Me.CombCurr.SelectedItem & "'", cnn)

                cnn.Open()
                Me.txtExchRate.Text = cmd.ExecuteScalar
                cnn.Close()

                Me.Cursor = Cursors.Default

                CalCurr()
            End If
        Catch ex As Exception
            Me.Cursor = Cursors.Default
            If cnn.State = ConnectionState.Open Then
                cnn.Close()
            End If
            MsgBox(ex.ToString)
        End Try
    End Sub

    Private Sub DTPDateOfSubmission_ValueChanged(sender As System.Object, e As System.EventArgs) Handles DTPDateOfSubmission.ValueChanged
        CheckDeadLine()
    End Sub
End Class